﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Eassets.Service.Global;
using Eassets.Layered.Domain.Global;
using Eassets.Service.Asset;
using Eassets.Layered.Domain.Asset;
using System.Collections.ObjectModel;


namespace Eassets.UI.Asset
{
    public partial class FixedAssetRegistryForm : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                GetOrganizationLevelList();
                GetAllMainCategory();
            }

            //GetAllMainCategory();

        }

        private void GetOrganizationLevelList()
        {
            OrganizationLevelListService organizationLevelListService = new OrganizationLevelListService();
            List<OrganizationLevelList> organizationLevelLists = new List<OrganizationLevelList>();
            organizationLevelLists = organizationLevelListService.GetAllOrganizationLevelList().Where(x => x.OrganizationLevelMode == "B").ToList();
            dropDownListOrganizationLevelList.DataSource = organizationLevelLists;
            dropDownListOrganizationLevelList.DataValueField = "OrganizationLevelListId";
            dropDownListOrganizationLevelList.DataTextField = "Code";
            dropDownListOrganizationLevelList.DataBind();
        }

        private void GetAllMainCategory()
        {
            CategoryService categoryService = new CategoryService();
            Category category = new Category();
            try
            {
                dropDownListMainCategory.DataSource = categoryService.GetAllAssetCategory().Where(x => x.ParentId == 0).ToList();

                dropDownListMainCategory.DataValueField = "CategoryId";
                dropDownListMainCategory.DataTextField = "Description";
                dropDownListMainCategory.DataBind();

            }
            catch (Exception exception)
            {
                throw;
            }
        }

        protected void dropDownListMainCategory_SelectedIndexChanged(object sender, EventArgs e)
        {
            GetAllSubCategory();
        }

        protected void dropDownListOrganizationLevelList_SelectedIndexChanged(object sender, EventArgs e)
        {
            AssetItemDetailsService assetItemDetailsService = new AssetItemDetailsService();
            Collection<AssetItemDetails> assetItemDetails = new Collection<AssetItemDetails>();
            AssetItemDetails assetItemDetail = new AssetItemDetails();
            
            if (Convert.ToInt64(dropDownListOrganizationLevelList.SelectedValue) > 0)
            {
                assetItemDetail.OrganizationLevelListId = Convert.ToInt64(dropDownListOrganizationLevelList.SelectedValue);
            }
            if (dropDownListAsset.SelectedValue!="")
            {
                assetItemDetail.AssetId = Convert.ToInt64(dropDownListAsset.SelectedValue);
            }
            else 
            {
                assetItemDetail.AssetId = 0;
            }
            assetItemDetails = assetItemDetailsService.GetAllAssetItemDetailsByOrganizationLevelListId(assetItemDetail.OrganizationLevelListId, assetItemDetail.AssetId);
            gridViewFixedAssetRegistry.DataSource = assetItemDetails;
            gridViewFixedAssetRegistry.DataBind();
        }

        protected void dropDownListSubCategory_SelectedIndexChanged(object sender, EventArgs e)
        {
            GetAssets();
        }


        private void GetAssets()
        {
            AssetService assetService = new AssetService();
            Assets assets = new Assets();
            try
            {
                dropDownListAsset.DataSource = assetService.GetAllAsset().Where(x => x.AssetCategoryId == Convert.ToInt32(dropDownListSubCategory.SelectedValue)).ToList();

                dropDownListAsset.DataValueField = "AssetId";
                dropDownListAsset.DataTextField = "Description";
                dropDownListAsset.DataBind();
            }
            catch (Exception exception)
            {
                throw;
            }
        }

        private void GetAllSubCategory()
        {
            CategoryService categoryService = new CategoryService();
            Category category = new Category();
            try
            {
                dropDownListSubCategory.DataSource = categoryService.GetAllAssetCategory().Where(x => x.ParentId == Convert.ToInt32(dropDownListMainCategory.SelectedValue)).ToList();

                dropDownListSubCategory.DataValueField = "CategoryId";
                dropDownListSubCategory.DataTextField = "Description";
                dropDownListSubCategory.DataBind();
            }
            catch (Exception exception)
            {
                throw;
            }
        }

        protected void dropDownListAsset_SelectedIndexChanged(object sender, EventArgs e)
        {
            DataBindToGridView();
        }

        protected void DataBindToGridView()
        {
            List<AssetItemDetails> assetItemDetails = new List<AssetItemDetails>();
            assetItemDetails = GetFixedAssetItemDetailsByAssetId(Convert.ToInt64(dropDownListAsset.SelectedValue)).ToList();
            assetItemDetails = (from AssetItemDetail in assetItemDetails where AssetItemDetail.AssetStatus != Convert.ToString("DIS") select AssetItemDetail).ToList();
            gridViewFixedAssetRegistry.DataSource = assetItemDetails;
            gridViewFixedAssetRegistry.DataBind();
        }

        protected Collection<AssetItemDetails> GetFixedAssetItemDetailsByAssetId(long assetId)
        {
            Collection<AssetItemDetails> assetItemDetails = new Collection<AssetItemDetails>();
            AssetItemDetailsService assetItemDetailsService = new AssetItemDetailsService();
            AssetItemDetails assetItemDetail = new AssetItemDetails();
            assetItemDetail.OrganizationLevelListId = Convert.ToInt64(dropDownListOrganizationLevelList.SelectedValue);
            assetItemDetail.AssetId = Convert.ToInt64(dropDownListAsset.SelectedValue);
            assetItemDetails = assetItemDetailsService.GetAllAssetItemDetailsByOrganizationLevelListId(assetItemDetail.OrganizationLevelListId, assetItemDetail.AssetId); ;
            return assetItemDetails;
        }

        protected void ButtonPrint_Click(object sender, EventArgs e)
        {
            ReportViewerForm reprotViewerForm = new ReportViewerForm();
            AssetItemDetails assetItemDetails = new AssetItemDetails();
            if (dropDownListOrganizationLevelList.SelectedValue != null)
            {
                assetItemDetails.OrganizationLevelListId = Convert.ToInt64(dropDownListOrganizationLevelList.SelectedValue);
            }
            if (dropDownListAsset.SelectedValue != "")
            {
                assetItemDetails.AssetId = Convert.ToInt64(dropDownListAsset.SelectedValue);
            }
            else 
            {
                assetItemDetails.AssetId = 0;
            }
            
            ScriptManager.RegisterStartupScript(this, typeof(string), "OPEN_WINDOW",
                "window.open( 'ReportViewerForm.aspx?OrganizationLevelListId=" + Convert.ToInt64(dropDownListOrganizationLevelList.SelectedValue) + "&AssetId=" + assetItemDetails.AssetId + "&mainform=FixedAssetRegistry&reqmode=none&com=Ranasinghe Enterprise & Construction pvt Ltd', null, 'height=400,width=800,status=yes,toolbar=no,menubar=no,location=no' );", true);
                                                    
        }
    }
}